%%-*- mode: erlang -*-

{mapping, "exhook.drivers", "emqx_extension_hook.drivers", [
  {datatype, string}
]}.

{mapping, "exhook.drivers.$name.$key", "emqx_extension_hook.drivers", [
  {datatype, string}
]}.

{translation, "emqx_extension_hook.drivers", fun(Conf) ->

    Filter  = fun(Opts) -> [{K, V} || {K, V} <- Opts, V =/= undefined] end,

    Duration = fun(S) ->
                   case cuttlefish_duration:parse(S, ms) of
                       Ms when is_integer(Ms) -> Ms;
                       {error, R} -> error(R)
                   end
               end,
    Integer = fun(S) -> list_to_integer(S) end,

    Atom = fun(S) -> list_to_atom(S) end,

    Python = fun(Prefix) ->
               [{init_module, Atom(cuttlefish:conf_get(Prefix ++ ".init_module", Conf, "main"))}, %% dirver
                {python_path, cuttlefish:conf_get(Prefix ++ ".path", Conf, undefined)},
                {call_timeout, Duration(cuttlefish:conf_get(Prefix ++ ".call_timeout", Conf, "5s"))}]
            end,

    Java = fun(Prefix) ->
             [{init_module, Atom(cuttlefish:conf_get(Prefix ++ ".init_module", Conf, "Main"))}, %% dirver
              {java_path, cuttlefish:conf_get(Prefix ++ ".path", Conf, undefined)},
              {call_timeout, Duration(cuttlefish:conf_get(Prefix ++ ".call_timeout", Conf, "5s"))}]
           end,

    Options = fun(python2) -> Filter(Python("exhook.drivers.python2"));
                 (python3) -> Filter(Python("exhook.drivers.python3"));
                 (java) -> Filter(Java("exhook.drivers.java"));
                 (_) -> error(not_supported_drivers_type)
              end,
    [{Atom(Name), Options(Atom(Name))} || Name <- string:tokens(cuttlefish:conf_get("exhook.drivers", Conf), ",")]
end}.
